const axios = require('axios')
// const Vue = require('vue')
const { Message } = require('element-ui');
// const router = require('../router/index');
module.exports = {
    namespaced: true,
    state: {
        classesData: [],
        studentsData: [],
        // 弹窗打开关闭
        dialogFormVisible: false,
        // 修改学生
        form: {
            _id: "",
            name: "",
            age: "",
            gender: "",
            classesId: "",
            imagePath:""
        },
        // 分页
        pageData: {
            pageSize: 4,
            currentPage: 1,
        }
    },
    getters: {},
    mutations: {
        getStudents(state, data) {
            state.studentsData = data
        },
        getclasses(state, data) {
            state.classesData = data
        },
        // 获取需要修改的学生数据
        updateStudents(state, row) {
            console.log(row)
            state.form = { ...row };
            state.dialogFormVisible = true;
        },
        // 关闭修改
        close(state) {
            state.dialogFormVisible = false;
            Message({
                type: "info",
                message: "已取消"
            });
        },



    },
    actions: {
        //列表渲染
        async getStudentsAsync(context,search) {

            let fy = {
                pageSize: context.state.pageData.pageSize,
                nowPage: context.state.pageData.currentPage,
                ...search
            }
            const { data } = await axios.get('/goods/getGoods',
            { params: fy })
            context.state.pageData.studentsTotal = data.total
            context.commit('getStudents', data)
        },
        //删除学生
        async getRemoveStudentAsync(context, _id) {
            console.info(_id)
            const { data } = await axios.delete('/goods', { data: { _id:_id } })
            if (data) {
                Message({
                    message: "删除成功！",
                    type: "success",
                    showClose: true,
                })
                if (context.state.studentsData.length == 1) {
                    context.state.pageData.currentPage -= 1
                }
                context.dispatch('getStudentsAsync')

            }
        },
        // 获取商品信息
        async getClassesData(context) {
            const { data } = await axios.get('/goods/getGoods')
            context.commit('getclasses', data)
        },
        // 新增商品
        async addGoods(context,goods) {
                alert("新增成功")
              const { data } = await axios.post("goods/addGoods",goods);
              context.dispatch('getStudentsAsync', data)
              
          },
        // 确认修改学生
        async sure(context, _id) {
            console.log(context.state.form)
                //   修改学生
                const { data } = await axios.post("/goods/updateGoods", context.state.form)
                if (data) {
                    context.state.dialogFormVisible = false;
                    Message({
                        type: "success",
                        message: "修改成功！"
                    });
                    context.dispatch('getStudentsAsync',data)
                } else {
                    Message.error("修改失败！");
                }
                
        },

        //改变当前pageSize
        handleSizeChange(context, val) {
            context.state.pageData.pageSize = val
            // console.log(context.state.pageData)
            context.dispatch('getStudentsAsync')
        },
        // 改变当前currentPage
        handleCurrentChange(context, val) {
            context.state.pageData.currentPage = val
            // console.log(context.state.pageData)
            context.dispatch('getStudentsAsync')
        },

    }
}